{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "51a45d73",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "os.environ['CUDA_VISIBLE_DEVICES'] = ''\n",
    "os.environ['TF_FORCE_GPU_ALLOW_GROWTH'] = 'true'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "00bbb168",
   "metadata": {},
   "outputs": [],
   "source": [
    "from transformers import AutoTokenizer, AutoModelForCausalLM\n",
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0be010bc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'huseinzol05-dummy-mistral-3b/checkpoint-4000'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from transformers.trainer_utils import get_last_checkpoint\n",
    "\n",
    "latest = get_last_checkpoint(\"huseinzol05-dummy-mistral-3b\")\n",
    "latest"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8577c334",
   "metadata": {},
   "outputs": [],
   "source": [
    "tokenizer = AutoTokenizer.from_pretrained('malaysia-ai/bpe-tokenizer')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a5960960",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d39fe798adf1484bab576975585cee92",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Loading checkpoint shards:   0%|          | 0/2 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "model = AutoModelForCausalLM.from_pretrained(latest)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "da9c6ab0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "from tqdm import tqdm\n",
    "\n",
    "kwargs = {\n",
    "    'temperature': 0.9, \n",
    "    'max_new_tokens': 20, \n",
    "    'top_p': 0.95,\n",
    "    'repetition_penalty': 1.1, \n",
    "    'do_sample': True,\n",
    "    'num_beams': 1,\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "27452c55",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0, 0, 0, 0, 0]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "inputs = tokenizer(['merupakan bekas Perdana Menteri Malaysia'], return_tensors='pt')\n",
    "generate_kwargs = dict(inputs)\n",
    "generate_kwargs = {**generate_kwargs, **kwargs}\n",
    "generate_kwargs.pop('token_type_ids', None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "a756d02b",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Setting `pad_token_id` to `eos_token_id`:2 for open-end generation.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "' merupakan bekas Perdana Menteri Malaysia, Tun Dr Mahathir Mohamad.\\\\nBeliau berkata, antara individu yang telah dipanggil untuk menghadiri program itu'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "o = model.generate(**generate_kwargs)\n",
    "tokenizer.decode(o[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b443f45d",
   "metadata": {},
   "outputs": [],
   "source": [
    "model = model.type(torch.bfloat16)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "85aedeeb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "476df6b0f62640fdb11f5542f55b8413",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Upload 2 LFS files:   0%|          | 0/2 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "223ee9427b6643d69caf5cea68c32bf9",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "model-00002-of-00002.safetensors:   0%|          | 0.00/1.07G [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "eeb308a09e6f4d8cb9b859f0a457a5b8",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "model-00001-of-00002.safetensors:   0%|          | 0.00/4.98G [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "CommitInfo(commit_url='https://huggingface.co/mesolitica/malaysian-mistral-3B-4096/commit/3de152b1e1b21dc3b63f2c4e6eb2210bec1ec695', commit_message='Upload MistralForCausalLM', commit_description='', oid='3de152b1e1b21dc3b63f2c4e6eb2210bec1ec695', pr_url=None, pr_revision=None, pr_num=None)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.push_to_hub('malaysian-mistral-3B-4096', organization='mesolitica', safe_serialization=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "3a8e23bb",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/ubuntu/.local/lib/python3.10/site-packages/transformers/utils/hub.py:671: UserWarning: The `organization` argument is deprecated and will be removed in v5 of Transformers. Set your organization directly in the `repo_id` passed instead (`repo_id={organization}/{model_id}`).\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "CommitInfo(commit_url='https://huggingface.co/mesolitica/malaysian-mistral-3B-4096/commit/b09ffd135d93181114440561deae0bfc0149146a', commit_message='Upload tokenizer', commit_description='', oid='b09ffd135d93181114440561deae0bfc0149146a', pr_url=None, pr_revision=None, pr_num=None)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tokenizer.push_to_hub('malaysian-mistral-3B-4096', organization='mesolitica', safe_serialization=True)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
